### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata_UCR <- read.dta("Analysis Data/UCR_analysis.dta")
mydata_UCR_LP <- filter(mydata_UCR, MP == 0)
mydata_UCR_MP <- filter(mydata_UCR, MP == 1)

# Panel A: All States UCR

T1_UCR <- feols(homicide_pc~ tMean + prec , data=mydata_UCR)
summary(T1_UCR, cluster="fips")
T2_UCR <- feols(homicide_pc~ tMean + prec | sample_month, data=mydata_UCR)
summary(T2_UCR, cluster="fips")
T3_UCR <- feols(homicide_pc~ tMean + prec | state_year , data=mydata_UCR)
summary(T3_UCR, cluster="fips")
T4_UCR <- feols(homicide_pc~ tMean + prec | ori_year, data=mydata_UCR)
summary(T4_UCR, cluster="fips")

models <- list(T1_UCR, T2_UCR, T3_UCR, T4_UCR)
modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_A4/Table_A4_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel B: "More-Prohibitive" Policy Regimes

T1_UCR_MP <- feols(homicide_pc~ tMean + prec , data=mydata_UCR_MP)
summary(T1_UCR_MP, cluster="fips")
T2_UCR_MP <- feols(homicide_pc~ tMean + prec | sample_month, data=mydata_UCR_MP)
summary(T2_UCR_MP, cluster="fips")
T3_UCR_MP <- feols(homicide_pc~ tMean + prec | state_year , data=mydata_UCR_MP)
summary(T3_UCR_MP, cluster="fips")
T4_UCR_MP <- feols(homicide_pc~ tMean + prec | ori_year, data=mydata_UCR_MP)
summary(T4_UCR_MP, cluster="fips")

models <- list(T1_UCR_MP, T2_UCR_MP, T3_UCR_MP, T4_UCR_MP)

modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_A4/Table_A4_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel C: "Less-Prohibitive" Policy Regimes

T1_UCR_LP <- feols(homicide_pc~ tMean + prec , data=mydata_UCR_LP)
summary(T1_UCR_LP, cluster="fips")
T2_UCR_LP <- feols(homicide_pc~ tMean + prec | sample_month, data=mydata_UCR_LP)
summary(T2_UCR_LP, cluster="fips")
T3_UCR_LP <- feols(homicide_pc~ tMean + prec | state_year , data=mydata_UCR_LP)
summary(T3_UCR_LP, cluster="fips")
T4_UCR_LP <- feols(homicide_pc~ tMean + prec | ori_year, data=mydata_UCR_LP)
summary(T4_UCR_LP, cluster="fips")

models <- list(T1_UCR_LP, T2_UCR_LP, T3_UCR_LP, T4_UCR_LP)

modelsummary(models, vcov = ~fips,  output = "Figures and Tables/Table_A4/Table_A4_Panel_C.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
